package com.lma.crm.coursetype.domain.web.action;

import org.apache.commons.lang3.StringUtils;

import com.lma.crm.coursetype.domain.CrmCourseType;
import com.lma.crm.coursetype.service.CrmCourseTypeService;
import com.lma.crm.page.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CrmCourseTypeAction extends ActionSupport implements ModelDriven<CrmCourseType> {
    
    private static final long serialVersionUID = -4282064459605962877L;
    
    //当前页
    private int pageNum = 1;
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    //页面显示条数
    private int pageSize = 2;
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    private CrmCourseType crmCourseType = new CrmCourseType();
    
    @Override
    public CrmCourseType getModel() {
        return crmCourseType;
    }

    private CrmCourseTypeService crmCourseTypeService;
    public void setCrmCourseTypeService(CrmCourseTypeService crmCourseTypeService) {
        this.crmCourseTypeService = crmCourseTypeService;
    }
    
    public String findAll() {
        
//        List<CrmCourseType> allCourseType = crmCourseTypeService.findAll(crmCourseType);
//        
//        ActionContext.getContext().put("allCourseType", allCourseType);
        
        PageBean<CrmCourseType> pageBean = this.crmCourseTypeService.findAll(crmCourseType, pageNum, pageSize);
        ActionContext.getContext().put("pageBean", pageBean);
        
        return "allCourseType";
    }

    public String addOrEditUI() {
        
        if(StringUtils.isNotBlank(this.crmCourseType.getCourseTypeId())) {
            //如果为编辑，会传递id，如果id不为空，将数据查询出来放入值栈中便于编辑页面回显
            CrmCourseType courseType = this.crmCourseTypeService.findById(this.crmCourseType.getCourseTypeId());
            ActionContext.getContext().getValueStack().push(courseType);
        }
        
        //如果id为空，直接前往编辑页面
        return "addOrEditUI";
    }
    
    public String addOrEdit() {
        
        this.crmCourseTypeService.addOrEdit(crmCourseType);
        
        return "addOrEdit";
    }
    
}
